package com.csmz.bxg.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.csmz.bxg.R;
import com.csmz.bxg.Utils.Md5Utils;

public class LoginActivity extends AppCompatActivity {
    private TextView tv_main_title;
    private TextView tv_back,tv_register,tv_find_psw;
    private Button btn_login;
    private String userName,psw,spPsw;
    private EditText et_user_name,et_psw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();

    }

    /**
     * 获取界面控件
     */
    private void init() {
        tv_main_title= (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("登陆");
        tv_back= (TextView) findViewById(R.id.tv_back);
        tv_find_psw= (TextView) findViewById(R.id.tv_find_psw);
        tv_register= (TextView) findViewById(R.id.tv_register);
        btn_login= (Button) findViewById(R.id.btn_login);
        et_user_name= (EditText) findViewById(R.id.et_user_name);
        et_psw= (EditText) findViewById(R.id.et_psw);

        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoginActivity.this.finish();
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });

        tv_find_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到找回密码界面
                Intent intent=new Intent(LoginActivity.this,FindPswActivity.class);
                startActivity(intent);
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName=et_user_name.getText().toString().trim(); //获取用户名
                psw=et_psw.getText().toString().trim();//获取密码
                String md5Psw= Md5Utils.md5(psw);//加密
                spPsw=readPsw(userName);//读取密码
                if (TextUtils.isEmpty(userName)){
                    Toast.makeText(LoginActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();return;
                }else if (TextUtils.isEmpty(psw)){
                    Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();return;
                }else if (md5Psw.equals(spPsw)){
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    //保存登陆状态和登陆的用户名
                    saveloginStatus(true,userName);
                    //把成功的状态传递到MainActivity中
                    Intent data=new Intent();
                    data.putExtra("isLogin",true);
                    setResult(RESULT_OK,data);
                    finish();
                    //startActivityForResult(new Intent(LoginActivity.this,MainActivity.class),1);
                    return;
                }else if ((!TextUtils.isEmpty(spPsw) && !md5Psw.equals(spPsw))){
                    Toast.makeText(LoginActivity.this,"输入的用户名和密码不一致",Toast.LENGTH_SHORT).show();return;
                }else{
                    Toast.makeText(LoginActivity.this,"此用户名不存在",Toast.LENGTH_SHORT).show();return;
                }


            }
        });
    }

    /**
     * 保存登陆状态的登陆用户名到SharedPreference
     *
     */
    private void saveloginStatus(boolean status,String userName) {
        SharedPreferences sp=getSharedPreferences("LoginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit(); //获取编辑器
        editor.putBoolean("isLogin",status);
        editor.putString("loginUserName",userName);//保存登陆时的用户名
        editor.commit();//提交修改
    }

    /**
     * 从SharedPreference中根据用户名读取密码
     */
    private String readPsw(String userName) {
        SharedPreferences sp=getSharedPreferences("LoginInfo",MODE_PRIVATE);
        //Log.d("csmz", "readPsw: "+sp.getString(userName,""));
        return sp.getString(userName,"");
    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (data!=null){
            //从注册界面传递过来的用户名
            String userName=data.getStringExtra("userName");
            if (!TextUtils.isEmpty(userName)){
                et_user_name.setText(userName);
                //设置光标的位置
                et_user_name.setSelection(userName.length());
            }
        }

    }
}
